/**
 * (c) Copyright Ascensio System SIA 2025
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zzvcom.office.sdk.manager;

import com.onlyoffice.manager.settings.DefaultSettingsManager;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Properties;

@Component
@Slf4j
public class SettingsManagerImpl extends DefaultSettingsManager {
    private static final String SETTINGS_PREFIX = "docservice";

   private static Properties properties;
    @Autowired
    private Environment environment;
    public SettingsManagerImpl() {

    }
    @PostConstruct
    public void init() {
        properties = new Properties();
    }
    @Override
    public String getSetting(final String name) {
        String key = SETTINGS_PREFIX + "." + name;
        // 优先从 Environment 读取（能获取到更全面的配置源）
        String value = environment.getProperty(key);
        if (value != null) {
            //log.info("getSetting from environment: name={}, value={}", key, value);
            return value;
        }
        // fallback 到当前的 properties
        value = properties.getProperty(key);
       //   log.info("getSetting from properties: name={}, value={}", key, value);
        return value;
    }

    @Override
    public void setSetting(final String name, final String value) {
       properties.put(SETTINGS_PREFIX + "." + name, value);
    }
}
